var cheerio = require('cheerio')
var superagent = require('superagent')
var url = require('url')
var async = require('async')
var eventproxy = require('eventproxy')
var ep = new eventproxy()
var mongoose = require('mongoose')
var mongourl = 'mongodb://127.0.0.1:27017'
require('superagent-proxy')(superagent);
var proxy = process.env.http_proxy || 'http://H46425Z6PY4A7JDD:EFAA7E0E1C10E6FE@proxy.abuyun.com:9020';
mongoose.Promise = global.Promise; 
mongoose.connect(mongourl,{useMongoClient:true});  
var Schema = mongoose.Schema;  
//骨架模版  
var movieSchema = new Schema({
    city:String
})  
var sanjiSchema = new Schema({
    city:String
})  
//模型  
var Movie = mongoose.model('Movie', movieSchema);  
var Sanji = mongoose.model('Sanji', sanjiSchema);  
//存储数据  




var suoyouerji=require('./erji.js')


var sanjis=[]
console.log("start")
async.mapLimit(suoyouerji,1,function(city, callback){
    console.log(city)
    superagent.get(city)
    .proxy(proxy)
    .end(function(err1,res1){
        if(res1){
            var $$ = cheerio.load(res1.text)
            console.log($$(".box.sidesubcat.t5 a").length)
            $$(".box.sidesubcat.t5 a").each(function(){
                sanjis.push($$(this).attr('href'))
                var sanji = new Sanji({city:$$(this).attr('href')})  
                sanji.save(function(err) {
                    if (err) {  
                        console.log('保存失败')  
                        return;  
                    }else{
                        console.log('ok')
                    }              
                }) 
            })
            console.log(sanjis.length+'='+city)
        }
    })
    setTimeout(function(){
        callback(null,city)
    },1000)
},function(err2,result2){
    setTimeout(function(){
        console.log(result2)
    },5000)
})



